﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="FinanceYTDReport.ascx.vb"
    Inherits="WebForm_ManageFinanceYTDReport_FinanceYTDReport" %>
<%@ Register Src="~/WebForm/ManageFinanceYTDReport/SearchOption.ascx" TagName="ProductHierarchyControl"
    TagPrefix="My" %>

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/WebForm/BLSService.asmx" />
    </Services>
</asp:ScriptManagerProxy>

<script>
function <%=Me.ClientID %>save()
{
    var h = $get('<%=Spreadsheet1Data.ClientID %>');
    h.value = htmlencode($get('Spreadsheet1').XmlData);
}

function <%=Me.ClientID %>js_LoadCompleted(sheetid)
{   
    $get(sheetid).Columns("C:BM").AutoFit();
    

}

function <%=Me.ClientID %>InitMemo()
{
    var sheet = $get('Spreadsheet1');
    var TargetCell = sheet.ActiveCell;
    var ItemNameCell = sheet.cells(TargetCell.row,2);
        var tc = new YTDMemo();
        
        if (TargetCell.column > 10) // signatures
        {
            tc.Signature = sheet.cells(2,TargetCell.column).value;
        }
        else // selected sig
        {
            tc.Signature = "<%= Me.Search1.SignatureText %>";        
        }        
        
        tc.Channel = '<%= Me.Search1.Channel %>';        
        if (tc.Channel.toLowerCase() == 'all') tc.Channel = '';
        
        tc.Hierarchy1 = '<%= Me.Search1.Hier1 %>';
        if (tc.Hierarchy1.toLowerCase() == 'all') tc.Hierarchy1 = '';
        
        tc.ItemName = ItemNameCell.value;
        
        tc.MonthCode = '<%= Me.Search1.Year %>';
       
        var year = parseInt(sheet.cells(2,TargetCell.column).value.substring(0,4));        
        if (!isNaN(year)) tc.MonthCode = year;
        //alert(tc.MonthCode);
        tc.MonIndex = '<%= Me.Search1.EndMonth %>';
        
        if (TargetCell.column == 5)
        {
            tc.MonIndex = 'T';
        }
        
        return tc;
}

function <%=Me.ClientID %>TC_GetMemo()
{
    //有註解欄位才取資料
    var txtNotes = $get('<%=Me.FindControl("txtNotes").ClientID %>');
    var btnUpload = $get('<%=Me.FindControl("btnUpload").ClientID %>');
    
    if (txtNotes == undefined) return;//1:Meeting

    var sheet = $get('Spreadsheet1');
    var TargetCell = sheet.ActiveCell;
    var ItemNameCell = sheet.cells(TargetCell.row,2);

    if (ItemNameCell.value!= undefined && TargetCell.row > 3 && sheet.cells(3,TargetCell.column) == 'NT$')
    {
        btnUpload.disabled = false;
        txtNotes.disabled = false;
        var tc = <%=Me.ClientID %>InitMemo();        

//alert(tc.MonthCode + ' ' + tc.MonIndex + ' ' + tc.ItemName);
        BLSService.GetYTDMemo(tc,<%=Me.ClientID %>ShowGetMemo);
    }
    else
    {
        txtNotes.value = "";
        btnUpload.disabled = true;
        txtNotes.disabled = true;
    }
}

function <%=Me.ClientID %>ShowGetMemo(arg)
{
    var txtNotes = $get('<%=Me.FindControl("txtNotes").ClientID %>');
    txtNotes.value = arg;
}


function <%=Me.ClientID %>TC_SaveMemo()
{
    var txtNotes = $get('<%=Me.FindControl("txtNotes").ClientID %>');
    if(txtNotes.value.length>=1000)
    {
        alert('Comments is too long!!(Length:1000)');
        return;
    }
    
    var sheet = $get('Spreadsheet1');
    var TargetCell = sheet.ActiveCell;
    var ItemNameCell = sheet.cells(TargetCell.row,2);

    if (ItemNameCell.value!= undefined && TargetCell.row > 3 && sheet.cells(3,TargetCell.column) == 'NT$')
    {
        var tc = <%=Me.ClientID %>InitMemo()
                
        tc.Memo = txtNotes.value;
        
        BLSService.SaveYTDMemo(tc,<%=Me.ClientID %>ShowSaveMemo);
    }
}

function <%=Me.ClientID %>ShowSaveMemo(arg)
{
    alert(arg);
}
</script>

    <script type="text/vbscript">
    Sub Spreadsheet1_LoadCompleted()
        Call <%=Me.ClientID %>js_LoadCompleted("Spreadsheet1")    
    End Sub
    
    'OK,滑鼠點擊事件
    Sub Spreadsheet1_Click()
        '紀錄現在的值
        set WorkSheet_SS = document.getElementById("Spreadsheet1")
        
        call <%=Me.ClientID %>TC_GetMemo()
    End Sub

    'OK,鍵盤上下左右事件
    Sub Spreadsheet1_KeyDown(ByVal KeyCode,ByVal Shift)    
        '紀錄現在的值
        set WorkSheet_SS = document.getElementById("Spreadsheet1")

        call <%=Me.ClientID %>TC_GetMemo()
    End Sub
        
    Sub Spreadsheet1_SelectionChange()
        set WorkSheet_SS = document.getElementById("Spreadsheet1")
        Dim Row 
	    Dim Col
	    MaxCol = WorkSheet_SS.ActiveSheet.UsedRange.Columns.Count 
        MaxRow = WorkSheet_SS.ActiveSheet.UsedRange.Rows.Count 
        sumvalue = 0 
        sumcount = 0 
        colType = "Number"
        for Each Row In WorkSheet_SS.Selection.Rows 
	        if Row.row > MaxRow then 
	            exit for 
            end if 
            for Each Col In WorkSheet_SS.Selection.Columns 
    	        if Col.column > MaxCol then 
 	 	            exit for 
                end if 
                if VarType(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value)>=2 and VarType(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value)<=5 then 
                    if sumcount=0 and InStr(WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).NumberFormat,"%")<>0 then
                        colType = "Percent"
                    end if
                    if colType = "Percent" then 
                        sumvalue = sumvalue + (WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value * 100)
                    else
                        sumvalue = sumvalue + WorkSheet_SS.ActiveSheet.Cells(Row.row,Col.column).value 
                    end if
                    sumcount = sumcount + 1
                end if 
            next 
        next 

        if sumcount<>0 Then
            if colType = "Percent" then
                avg = Round(sumvalue / sumcount,2)
            else
                avg = Round(sumvalue / sumcount,0)
            end if
        else
            avg = 0
        end if
        if colType = "Percent" then
            window.status = "Average: " & FormatNumber(avg,2) & "%" & "  Count: " & FormatNumber(sumcount,0) & "  Sum: " & FormatNumber(sumvalue,2) & "%"
        else
            window.status = "Average: " & FormatNumber(avg,0) & "  Count: " & FormatNumber(sumcount,0) & "  Sum: " & FormatNumber(sumvalue,0)
        end if
    End Sub     
    </script>

<My:ProductHierarchyControl ID="Search1" runat="server" />
<br />
<asp:PlaceHolder ID="pl" runat="server" Visible="false">
    <div style="background-color: #351D26">
        <fieldset id="pnlData" runat="server" style="border-color: #4E333B;">
            <legend class="white2">
                <asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:pnlData.GroupingText%>" />
            </legend>
        </fieldset>
        <asp:Button ID="btnSave" SkinID="custom" CssClass="button2" runat="server" Width="70" meta:resourcekey="btnSave" OnClientClick='<%# Me.ClientID + "save();" %>'
            OnClick="btnSave_Click" Enabled='<%# Search1.Signature <> String.Empty And Search1.Channel <> MyObj.GetDDLSelectAllItemV2.Text And Search1.Hier1 <> MyObj.GetDDLSelectAllItemV2.Text %>' />
        <ajaxToolkit:ConfirmButtonExtender ID="cbSave" runat="server" meta:resourcekey="cbSave"
            TargetControlID="btnSave">
        </ajaxToolkit:ConfirmButtonExtender>
        &nbsp;
        <asp:Label ID="lblNotes" Font-Bold="true" runat="server" meta:resourcekey="lblNotes"></asp:Label>
        <asp:TextBox ID="txtNotes" TextMode="MultiLine" Rows="2" Columns="38" runat="server"
            Width="500px"></asp:TextBox> &nbsp;            
        <input type="button" id="btnUpload" class="button2" runat="server" value="<%$ Resources:btnUpload.Text %>" style="width:70px;"
            onclick='<%# Me.ClientID + "TC_SaveMemo();return false;" %>' />
    </div>
    <br />
    <object id="Spreadsheet1" classid="CLSID:0002E559-0000-0000-C000-000000000046" width="100%"
        height="600" standby="Loading">
        <param name="DataType" value="XMLURL" />
        <param name="XMLURL" value="<%= MyObj.xlsUrl %>" />
    </object>
    <asp:HiddenField ID="Spreadsheet1Data" runat="server" />
</asp:PlaceHolder>
